<template>
	<view>
		<view class="top">
			<view class="logo">
				<image src="/static/logo.png" mode=""></image>
			</view>
			
		</view>
		<view class="form">
			<view class="form-item">
				<text class="front">+86</text>
				<input  type="number" placeholder="请输入手机号" placeholder-class="ipt-placeholder" v-model="phone">
				<view class="get-code" @click="getCode">
					<text>{{yzText}}</text>
				</view>
			</view>
			<view class="form-item">
				<input  type="number" placeholder="请输入验证码" placeholder-class="ipt-placeholder" v-model="code">
			</view>
			<view class="form-item">
				<input v-if="!pswShow" type="password" placeholder="设置密码（8-16 字符）" placeholder-class="ipt-placeholder" v-model="psw">
				<input v-if="pswShow" type="text" placeholder="设置密码（8-16 字符）" placeholder-class="ipt-placeholder" v-model="psw">
				<view class="eye-box">
					<image @click="pswShow = !pswShow" :src="pswShow?'/static/images/login/eye.png':'/static/images/login/ey-close.png'" mode="" class="eye"></image>
				</view>
			</view>
			<view class="form-item">
				<input v-if="!pswShow"  type="password" placeholder="再次输入密码（8-16 字符）" placeholder-class="ipt-placeholder" v-model="pswAgain">
				<input v-if="pswShow"   type="text" placeholder="再次输入密码（8-16 字符）" placeholder-class="ipt-placeholder" v-model="pswAgain">
				<view class="eye-box">
					<image @click="pswShow = !pswShow" :src="pswShow?'/static/images/login/eye.png':'/static/images/login/ey-close.png'" mode="" class="eye"></image>
				</view>
			</view>
		</view>
		<view class="register_btn" @click="confirm">
			注册
		</view>
		<view class="login" @click="backLogin">
			去登录
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:'',
				psw:'',
				pswAgain:'',
				code:'',
				pswShow:false,
				yzText:'获取验证码',
				countdown:60,
				cTimer: null,
				codeEnable:true,
			};
		},
		methods:{
			confirm(){
				console.log('=======','注册');
				if(!this.phone){
					uni.showToast({
						title: '请输入手机号',
						icon:"none"
					})
					return
				}
				if(!this.code){
					uni.showToast({
						title: '请输入验证码',
						icon:"none"
					})
					return
				}
				
				if(!this.psw){
					uni.showToast({
						title: '请输入密码',
						icon:"none"
					})
					return
				
				}
				
				if(this.psw != this.pswAgain){
					uni.showToast({
						title: '两次密码不一致',
						icon:"none"
					})
					return
				}
				let param = {
					mobile:this.phone,
					code:this.code,
					password:this.psw
				};
				console.log('-==-==',param);
				this.http.post('user/register',param).then(res=>{
					console.log('res--',res);
					if(res.code ==1){
						uni.showToast({
							title: '注册成功',
							icon:"none"
						})
						// uni.reLaunch({
						// 	url:'/pages/login/applogin'
						// })
					}else{
						uni.showToast({
							title: res.msg,
							icon:"none"
						})
					}
				});
			},
			getCode(){
				if(!this.codeEnable){
					return
				}
				if(!this.phone){
					uni.showToast({
						title: '请输入正确的手机号码',
						icon:"none"
					})
					return
				}
				this.http.post('user/getphonecode',{mobile:this.phone}).then(res=>{
					console.log('res--',res);
					if(res.code ==1){
						uni.showToast({
							title: '验证码已发送',
							icon:"none"
						})
						this.code = res.data.code
					}else{
						uni.showToast({
							title: res.msg,
							icon:"none"
						})
					}
				})
				this.startTimer()
				
			},
			//定时器
			startTimer() {
				if (this.cTimer == null) {
					this.cTimer = setInterval(() => {
						this.countdown--;
						if (this.countdown == 0) {
							this.yzText = '重新获取'
							this.codeEnable = true
							this.clearTimer();
						}else{
							this.codeEnable = false
							this.yzText =this.countdown+ '秒重新获取'
						}
					}, 1000)
				}
			},
			clearTimer() {
				this.countdown = 60;
				clearInterval(this.cTimer);
				this.cTimer = null;
			},
			backLogin(){
				uni.navigateBack()
			}
			
		}
	}
</script>

<style lang="scss">
	.top{
		.title{
			width: 750rpx;
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #000000;
		}
		.logo{
			width: 750rpx;
			display: flex;
			justify-content: center;
			margin-top: 120rpx;
			image{
				width: 176rpx;
				height: 176rpx;
			}
		}
	}
	
	.form{
		width: 750rpx;
		padding:0 64rpx ;
		margin-top: 200rpx;
		box-sizing: border-box;
	}
	.form-item{
		width: 100%;
		padding: 30rpx 0;
		border-bottom: 1px solid #f1f1f1;
		position: relative;
		display: flex;
		.eye-box{
			height: 100%;
			display:flex;
			width: 44rpx;
			align-items: center;
			position: absolute;
			right: 0;
			bottom: 0;
		}
		.eye{
			width: 44rpx;
			height: 30rpx;
			
			
		}
	}
	.front{
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #000000;
		margin-right: 30rpx;
	}
	.ipt-placeholder{
		color: #dfdfdf;
		font-size: 32rpx;
	}
	.register_btn{
		height: 88rpx;
		width: 622rpx;
		margin-left: 64rpx;
		line-height: 80rpx;
		text-align: center;
		background-color: $uni-color-primary;
		border-radius: 8rpx;
		margin-top: 160rpx;
		color: #fff;
	}
	.get-code{
		position: absolute;
		height: 100%;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		color: #4A9CE7;
		font-size: 28rpx
;
	}
	.login{
		width: 100%;
		text-align: center;
		margin-top: 30rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #000000;
	}
</style>
